package sis.ra.yahoor.result;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;

import sis.ra.utility.Utils;


public class stemmingforVerb {
	public static void stem (String text)
	{
		String linguistxpath="c:\\Program Files\\INXIGHT\\LinguistxPlatform3.7\\lx-3\\";
		String htformat="text";
		Utils.writeFile(linguistxpath+"windows-2003-i686-msvc-7.1-32\\test.txt ", text);
		String s="";
		HashMap<String, Integer> verblist=new HashMap<String, Integer>();
		String cmd="\""+linguistxpath+"windows-2003-i686-msvc-7.1-32\\test_platform.exe\" -k 735442535d524e "+
		"-d " + "\""+linguistxpath + "lang\"" + 
		" -stem " +
		" -"+htformat+" " +"\"" +linguistxpath + "windows-2003-i686-msvc-7.1-32\\test.txt\" ";
		try {
//			System.out.println(cmd);
		     Process p = Runtime.getRuntime().exec(cmd);
		    
		    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
		    BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
		    while ((s = stdInput.readLine()) != null)   
		    {
//		    	System.out.println(s);
		    	String []ts=s.split("\t");
		    	if (ts.length==2)
		    	{
		    		String verb=ts[1].trim();
		    		if (ts[1].contains("|"))
		    		{
		    			verb=ts[1].split("\\|")[0].trim();
		    		}
//		    		System.out.println("++>"+verb);
		    		if (verblist.keySet().contains(verb))
		    		{
		    			Integer count=verblist.get(verb);
		    			verblist.remove(verb);
		    			verblist.put(verb, count+1);
		    		}else
		    			verblist.put(verb, 1);
		    	}
//		    	if (s.startsWith(prefix))
		    }
//		    while ((s = stdError.readLine())!=null)
//		    {
//		    	System.out.println(s);
//		    }
		}
		catch (IOException e) {
			    e.printStackTrace();    
		}
		
		HashMap<Integer, Integer> verbfreq=new HashMap<Integer,Integer>();
		
		Iterator <String> verbs=verblist.keySet().iterator();
		while (verbs.hasNext())
		{
			String verb=verbs.next();
			System.out.println(verb+"\t"+verblist.get(verb));
			
			if (verbfreq.keySet().contains(verblist.get(verb)))
			{
				int cur=verbfreq.get(verblist.get(verb));
				verbfreq.remove(verblist.get(verb));
				verbfreq.put(verblist.get(verb), cur+1);
			}
			else
			{
				verbfreq.put(verblist.get(verb), 1);
			}
		}
		
		Iterator <Integer> verbfre=verbfreq.keySet().iterator();
		while (verbfre.hasNext())
		{
			Integer verbfr=verbfre.next();
			System.out.println(verbfr+"\t"+verbfreq.get(verbfr));
		}
	
	}
	public static void main(String[] args) {
		String verblist="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verblist";
		String content=Utils.readFile(verblist).toLowerCase();
//		String [] terms=content.split("\t");
//		for (int i=0;i<terms.length;i++)
//		{
//			if (terms.length<1) continue;
//			System.out.println(terms[i]);
//		}
		
		stem(content);
	}

}
